package com.gxuwz.controller;

import com.gxuwz.model.entity.Instructor;
import com.gxuwz.model.vo.Result;
import com.gxuwz.service.InstService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/instructors")
@CrossOrigin
public class InstController {

    @Autowired
    private InstService instService;

    // 获取所有辅导员
    @GetMapping("/list")
    public Result<List<Instructor>> listInstructors() {
        List<Instructor> instructors = instService.getAllInstructors();
        return Result.succeed(instructors);
    }

    // instID 和 instName查询获取辅导员信息
    @GetMapping("/instID/{instID}")
    public Result<Instructor> getInstructorByInstId(@PathVariable String instID) {
        Instructor instructor = instService.getInstructorByInstId(instID);
        return instructor != null ? Result.succeed(instructor) : Result.error("未找到该辅导员");
    }
    @GetMapping("/instName/{instName}")
    public Result<Instructor> getInstructorByInstName(@PathVariable String instName) {
        Instructor instructor = instService.getInstructorByInstName(instName);
        return instructor != null ? Result.succeed(instructor) : Result.error("未找到该辅导员");
    }

    // 添加辅导员
    @PostMapping("/add")
    public Result addInstructor(@RequestBody Instructor instructor) {
        boolean result = instService.addInstructor(instructor);
        return result ? Result.succeed("辅导员添加成功") : Result.error("辅导员添加失败");
    }

    // 更新辅导员信息
    @PutMapping("/update")
    public Result updateInstructor(@RequestBody Instructor instructor) {
        System.out.println(instructor.getInstID());
        boolean result = instService.updateInstructor(instructor);
        return result ? Result.succeed("辅导员信息更新成功") : Result.error("辅导员信息更新失败");
    }

    // 删除辅导员
    @DeleteMapping("/delete/{instID}")
    public Result deleteInstructor(@PathVariable String instID) {
        boolean isDeleted = instService.deleteInstructorByInstId(instID);
        return isDeleted ? Result.succeed("辅导员删除成功") : Result.error("辅导员删除失败");
    }
}
